<script setup lang="ts">

//头部
import {Refresh, Search} from "@element-plus/icons-vue";
import {reactive, ref} from "vue";
import {dayjs, ElMessage, FormInstance, UploadProps} from "element-plus";

//图片定义请求头
let headers = ref({'Authorization': getToken()})

const formInline = reactive({
    user: '',
    region: '',
    articleInput: ''
})
//展示列表
const article = reactive({
    articleInput: ''
})

//主体
import index from '@/components/Editor/index.vue'
import {
    addArticles,
    articleType,
    delArticles,
    detailsArticles,
    editArticles,
    viewArticles
} from "@/api/interface/reuqest";
import {handleCurrentChange} from "element-plus/es/components/tree/src/model/util";
import {getToken} from "@/utils/auth";

let tableData = ref<any>([])
let currentPage = ref<number>()

//重置
const ruleFormRef = ref<FormInstance>()
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
    articles = {
        pageNum: 1,
        pageSize: 10,
        title: ''
    }
    getArticlesList()
}
//添加文章
const addArticle = ref(false)
const delArticle = ref(false)
const editArticle = ref(false)
const detailsArticle = ref(false)

let form = reactive({
    title: '',
    img: '',
    content: '',
    typeId: ''
})

//上传图片
const imageUrl = ref('')

const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
    imageUrl.value = URL.createObjectURL(uploadFile.raw!)
    // console.log(response)
    form.img = response.url
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png') {
        ElMessage.error('只能上传jpg或png格式的文件')
        return false
    }
    return true
}

//文本框
const textarea = ref('')

//分页器
let total
//展示列表
let obj
let articles = {
    pageNum: 1,
    pageSize: 10,
    title: ''
}

function getArticlesList() {
    viewArticles(articles).then((item) => {
        if (item.code === 200) {
            console.log(item.rows)
            tableData.value = item.rows.map((value, index) => {
                // console.log(value,index)
                obj = {
                    serial: index + 1,
                    title: value.title,
                    author: value.author,
                    time: value.sendTime,
                    id: value.id,
                    content: value.content,
                    titlePicture: value.titlePicture,
                    categoryId: value.categoryId
                }
                // console.log(obj)
                return obj;
            })
            // console.log(tableData.value)
            total = item.total
        }
        // console.log(item)
    })
}

getArticlesList()

//分页器
function handleCurrentChange(newPage) {
    // console.log(newPage)
    articles = {
        pageNum: newPage,
        pageSize: 10,
        title: ''
    }
    getArticlesList()
}

//搜索
function articleList() {
    articles = {
        pageNum: 1,
        pageSize: 10,
        title: article.articleInput
    }
    getArticlesList()
}

//点击搜索
function searchArticle() {
    getArticlesList()
}

//新增
let articleTypeList = ref([])
let articleTypeObj = {}

//添加文章
function addListExpansion() {
    let typeObj = {
        pageNum: 1,
        pageSize: 1000
    }
    addArticle.value = true
    //文章分类
    articleType(typeObj).then((item) => {
        // console.log(item)
        articleTypeList.value = item.rows.map((value) => {
            // console.log(value)
            articleTypeObj = {
                name: value.name,
                id: value.id
            }
            // console.log(articleTypeObj.value)
            return articleTypeObj
        })
        // console.log(articleTypeList.value)
    })
}

function addArticleList() {
    let articleObj = {
        title: form.title,
        titlePicture: form.img,
        categoryId: form.typeId,
        content: form.content,
        sendTime: dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss'),
        weight: 0
    }
    if (articleObj.title.trim().length!=0  && articleObj.content.trim().length!=0) {
        addArticles(articleObj).then((item) => {
            // console.log(item)
            if(item.code == 200){
                form.img = ''
                form.content = ''
                addArticle.value = false
                ElMessage({
                    type:'success',
                    message: '添加成功'
                })
                getArticlesList()
            }
        })

    }
        else{
        ElMessage({
            showClose: true,
            message: '标题或内容不能为空',
            type: 'error',
        })
    }
}

//删除文章
let delId

function delListExpansion(data) {
    console.log(data)
    delId = data.id
    delArticle.value = true
}

function delArticleList() {
    delArticles(delId).then((item) => {
        if (item.code === 200) {
            console.log(item)
            getArticlesList()
        }
    })
    delArticle.value = false
}

//详情文章
let detailsContent = ref()
let detailsTitle = ref()

function detailsExpansion(data) {
    detailsArticle.value = true
    detailsContent.value = data.content
    detailsTitle.value = data.title
    // console.log(detailsContent)
}

//编辑文章
const formEdit = ref({
    title: '',
    img: '',
    content: '',
    categoryId: ''
})
let editObj = ref({})

function editListExpansion(data) {
    //文章分类
    let typeObj = {
        pageNum: 1,
        pageSize: 1000
    }
    articleType(typeObj).then((item) => {
        // console.log(item)
        articleTypeList.value = item.rows.map((value) => {
            // console.log(value)
            articleTypeObj = {
                name: value.name,
                id: value.id
            }
            // console.log(articleTypeObj.value)
            return articleTypeObj
        })
        // console.log(articleTypeList.value)
    })
    editArticle.value = true
    formEdit.value = data
    editObj.value = data
    console.log(data)
}

function editArticleList() {
    let articleObj = {
        id: editObj.value.id,
        title: editObj.value.title,
        titlePicture: editObj.value.titlePicture,
        categoryId: editObj.value.categoryId,
        content: editObj.value.content
    }
    editArticles(articleObj).then((item) => {
        // console.log(item)
        if(item.code == 200){
            ElMessage({
                type:'success',
                message: '修改成功'
            })
        }
    })
    editArticle.value = false
}
const formRef = ref<FormInstance>()
</script>

<template>
    <div class="box">
        <!--        头部-->
        <div>
            <el-form :inline="true" :model="article" class="demo-form-inline" ref="ruleFormRef">
                <el-form-item label="文章名称" prop="articleInput">
                    <el-input v-model="article.articleInput" style="width: 180px" placeholder="请输入文章名称"
                              @input="articleList"
                              clearable/>
                </el-form-item>
                <el-form-item class="button">
                    <el-button type="primary" :icon="Search" @click="searchArticle">搜索</el-button>
                    <el-button :icon="Refresh" @click="resetForm(ruleFormRef)">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <!--        主体-->
        <div style="position: relative">
            <el-form-item>
                <el-button type='primary' @click="addListExpansion">
                    添加
                </el-button>
            </el-form-item>
            <el-table :data="tableData" style="width: 100%;">
                <el-table-column prop="serial" label="序号" width="250" align="center"/>
                <el-table-column prop="title" label="文章名称" width="300" align="center"/>
                <el-table-column prop="author" label="发布人" width="300" align="center"/>
                <el-table-column prop="time" label="创建时间" width="300" align="center"/>
                <el-table-column label="操作" width="280" align="center">
                    <template #default="scope">
                        <el-button link type="primary" size="small" @click="detailsExpansion(scope.row)">详情
                        </el-button>
                        <el-button link type="primary" size="small" @click="editListExpansion(scope.row)">编辑
                        </el-button>
                        <el-button link type="primary" size="small" @click="delListExpansion(scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--            分页器-->
            <el-pagination background layout="prev, pager, next" :total="total" :current-page.sync="currentPage"
                           @current-change="handleCurrentChange" class="page"/>
        </div>
        <!--        添加弹框-->
        <el-dialog v-model="addArticle" title="添加文章  " width="1000" center="true">
            <el-form :model="form" ref="formRef">
                <el-form-item label="文章标题" style="font-weight: 200">
                    <el-input v-model="form.title" placeholder="请输入文章名称" clearable/>
                </el-form-item>
                <div class="windowSection">
                    <div class="sectionTop">
                        <el-form-item label="封面图片">
                            <el-upload
                                class="avatar-uploader"
                                action="/dev-api/common/upload"
                                :headers="headers"
                                :show-file-list="false"
                                :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload">
                                <img v-if="imageUrl" :src="imageUrl" class="avatar"/>
                                <el-icon v-else class="avatar-uploader-icon">
                                    <Plus/>
                                </el-icon>
                            </el-upload>
                        </el-form-item>
                        <el-form-item label="选择文章栏目" style="margin-left: 50px">
                        </el-form-item>
                        <el-select
                            v-model="form.typeId"
                            placeholder="请选择"
                            clearable
                        >
                            <el-option v-for="item in articleTypeList" :key="item.id" :label="item.name"
                                       :value="item.id"/>
                        </el-select>
                    </div>
                    <el-form-item label="文章内容"></el-form-item>
                    <index style="height: 200px;margin-bottom: 30px" v-model="form.content"></index>
                </div>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="addArticle = false">取消</el-button>
                    <el-button type="primary" @click="addArticleList(formRef)">
                        保存
                    </el-button>
                </div>
            </template>
        </el-dialog>
        <!--        删除弹框-->
        <el-dialog
            v-model="delArticle"
            title="提示"
            width="400"
            align-center
        >
            <el-icon :size="20" color="#FFBA00">
                <WarningFilled/>
            </el-icon>
            <span style="position: relative;top:-4px;left: 8px">此操作将永久删除该文章，是否继续？</span>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="delArticle = false">取消</el-button>
                    <el-button type="primary" @click="delArticleList">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>
        <!--        详情弹框-->
        <el-dialog
            v-model="detailsArticle"
            title="文章详情"
            width="800"
            align-center
        >
            <div v-html="detailsTitle" style="font-size: 20px;font-weight: 700;text-align: center;color: black"></div>
            <div v-html="detailsContent" style="margin: 20px 20px 0;padding: 25px;background-color: #FAFAFA;"></div>
        </el-dialog>
        <!--        编辑弹框-->
        <el-dialog v-model="editArticle" title="编辑文章  " width="1000" center="true">
            <el-form :model="formEdit">
                <el-form-item label="文章标题" style="font-weight: 200">
                    <el-input v-model="formEdit.title" placeholder="请输入文章名称" clearable/>
                </el-form-item>
                <div class="windowSection">
                    <div class="sectionTop">
                        <el-form-item label="封面图片">
                            <el-upload
                                class="avatar-uploader"
                                action="/dev-api/common/upload"
                                :headers="headers"
                                :show-file-list="false"
                                :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload">
                                <img v-if="imageUrl" :src="imageUrl" class="avatar"/>
                                <el-icon v-else class="avatar-uploader-icon">
                                    <Plus/>
                                </el-icon>
                            </el-upload>
                        </el-form-item>
                        <el-form-item label="选择文章栏目" style="margin-left: 50px">
                        </el-form-item>
                        <el-select
                            v-model="formEdit.categoryId"
                            placeholder="请选择"
                            clearable
                        >
                            <el-option v-for="item in articleTypeList" :key="item.id" :label="item.name"
                                       :value="item.id"/>
                        </el-select>
                    </div>
                    <el-form-item label="文章内容"></el-form-item>
                    <index style="height: 200px;margin-bottom: 30px" v-model="formEdit.content"></index>
                </div>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="editArticle = false">取消</el-button>
                    <el-button type="primary" @click="editArticleList">
                        保存
                    </el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<style scoped lang="scss">
.box {
    padding: 30px;
    margin-bottom: 30px;
}

.page {
    position: absolute;
    right: 10px;
    bottom: -50px;
    //margin-top: 20px;
    //margin-left: 1000px;
}

.sectionTop {
    display: flex;
}

//上传图片
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

//文本框
::v-deep(.el-dialog__header) {
    position: relative;
    background-color: #1892FF;

    .el-dialog__headerbtn {
        height: 54.67px;
        position: absolute;
        top: 0px;
        right: -16px;
        background-color: #1892FF;

        svg {
            color: white;
        }
    }

    span {
        color: #FFFFFF;
    }
}
</style>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>
